package spring6_learn.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("spring6_learn.reflect.User");
        String name = "age";
        String setname ="set"+ name.toUpperCase().charAt(0) + name.substring(1);
        Field filed=clazz.getDeclaredField( name);//获取属性类型
        Method setAge = clazz.getDeclaredMethod(setname, filed.getType());
        Object user = clazz.newInstance();
        setAge.invoke(user, 18);
        System.out.println(user);
    }
}
